package edu.uw.tcss360.group6.gui.MainCards;

import java.awt.Color;
import java.awt.Component;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.IOException;


import javax.swing.BorderFactory;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;


import edu.uw.tcss360.group6.gui.GUI;

import edu.uw.tcss360.group6.model.*;
/**
 * Project: cd1 phase
 * Group 6- Ambi Adla
 * Author: Mais Abdulwahab
 * Date: 2/24/13
 */
@SuppressWarnings("serial")
public class EntryPanel extends MainCard{
	private JButton my_register = new JButton("Submit");
	private JTextArea my_descfield = new JTextArea(5, 20);
	private String my_category_name;
	private final JButton my_weaving_draft;
	
    public EntryPanel() {
        super();
        GridLayout bl = new GridLayout(5,2);
        this.setLayout(bl);
        addElement(new JLabel("Weaving Draft"));
        my_weaving_draft = new JButton("Weaving Draft");
        my_weaving_draft.addActionListener(new ActionListener(){
    	   public void actionPerformed(final ActionEvent the_event)
    	   {
    		   GUI.instance().my_main_panel.setCard("weaving-draft");
    		   GUI.instance().my_side_panel.datacardpanel.getAttendeeCard().enableSubmission();
    		   GUI.instance().my_side_panel.datacardpanel.getContestantCard().enableSubmission();
    		   GUI.instance().setFlag(true);
    	   }
    	   });
        addElement(my_weaving_draft);
        addElement(new JLabel("Select A category: "));
        addCategories();
        addElement(new JLabel("Brief Description :"));
        JScrollPane desc_scrollPane = new JScrollPane(my_descfield);
        my_descfield.setBackground(Color.yellow);
        addElement(desc_scrollPane);
     
       my_register.addActionListener(new ActionListener()
       {
    	   @SuppressWarnings("static-access")
		public void actionPerformed(final ActionEvent an_event)
    	   {
    		   String my_desc_string = my_descfield.getText();
    		   if(my_desc_string.isEmpty() || my_category_name == null)
    		   {
    			   JOptionPane.showMessageDialog(null, "Enter the missing info!");
    		   }
    		   else
    		   {
    			   JOptionPane.showMessageDialog(null, "Entry submitted correctly!");
    			   my_descfield.setText("");
    			   
    			   GUI.instance().my_main_panel.setCard(GUI.instance().currentUser.getClass().getSimpleName().toLowerCase() + "-splash");
                   try {
                        GUI.instance().currentUser = Contest.instance().userToContestant(GUI.instance().currentUser);
                   } catch(Exception e) {
                       //ensure the user is a contestant
                       JOptionPane.showMessageDialog(null, "You aren't allowed to submit!");
                       return;
                   }
                   if(Contest.instance().getCategory(my_category_name) == null)
                   {
                	   JOptionPane.showMessageDialog(null, "This is not a valid category!");
                   }
    
    			   Contest.instance().addEntry(new Entry(GUI.instance().my_main_panel.getWeavingPanel().getWeavingDraft(),Contest.instance().getCategory(my_category_name),my_desc_string,(Contestant)GUI.instance().currentUser));
    			   try {
					Contest.instance().save();
				} catch (IOException e) {
					e.printStackTrace();
					System.out.println("Couldn't save the new user");
				}
    		   }
    		   
    	   }
    	   
       });
        addElement(my_register);
        setBorder(BorderFactory.createEmptyBorder(180, 10, 150, 5));
       
    }
    private void addCategories()
    {
    	Category[] cat = Contest.instance().getCategories();
    	String[]temp = new String[cat.length];
    	for(int i = 0; i < cat.length; i++)
    	{
    		temp[i] = cat[i].getName();
    	}
    	JComboBox category_list = new JComboBox(temp);
    	category_list.addActionListener(new ActionListener()
    	{
    		public void actionPerformed(final ActionEvent an_event)
    		{
    			JComboBox cb = (JComboBox)an_event.getSource();
    	        my_category_name = (String)cb.getSelectedItem();
    		}
    		
    	});
    	addElement(category_list);
    }

    private void addElement(JComponent c) {
        c.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.add(c);
    }
    
   

}
